#ifndef user_h
#define user_h


struct point {
  int x, y;
  char xv, yv;
};
static struct point triangle[6]; // use fourth component to draw rectangle

// Restart the drawing
static void restart()
{
  // Clear the screen
  //GD.fill(RAM_SPRIMG, 0, 16384);

  clear_screen(); 

  // Position triangle at random
  byte i;
  for (i = 0; i < 3; i++) 
  {
    triangle[i].x = 3 + random(250);
    triangle[i].y = 3 + random(250);
  }
  
  // do also a rectangle 
  triangle[3].x = 3 + random(250);
  triangle[3].y = 3 + random(250);
  
  // or even more ?
  triangle[4].x = 3 + random(250);
  triangle[4].y = 3 + random(250);
  
  
  triangle[0].xv = 1;
  triangle[0].yv = 1;
  triangle[1].xv = -1;
  triangle[1].yv = 1;
  triangle[2].xv = 1;
  triangle[2].yv = -1;
  
  triangle[3].xv = -1;
  triangle[3].yv = -1;
 
  triangle[4].xv = 1;
  triangle[4].yv = 1;

  // RGB(0,0,0)

  // Choose a random palette
  GD.wr16(PALETTE4A + 0, RGB(0,0,64)); // this is the background color
  GD.wr16(PALETTE4A + 2, RANDOM_RGB());
  GD.wr16(PALETTE4A + 4, RANDOM_RGB());
  GD.wr16(PALETTE4A + 6, RANDOM_RGB());
}

void setup_user_1()
{
  int i;

  GD.begin();
  GD.ascii();

  GD.putstr(0, 0,"USER SPACE SW #1");
  
  // Draw 256 sprites left to right, top to bottom, all in 4-color
  // palette mode.  By doing them in column-wise order, the address
  // calculation in setpixel is made simpler.
  // First 64 use bits 0-1, next 64 use bits 2-4, etc.
  // This gives a 256 x 256 4-color bitmap.

  for (i = 0; i < 256; i++) {
    int x =     72 + 16 * ((i >> 4) & 15);
    int y =     22 + 16 * (i & 15);
    int image = i & 63;     /* image 0-63 */
    int pal =   3 - (i >> 6);   /* palettes bits in columns 3,2,1,0 */
    GD.sprite(i, x, y, image, 0x8 | (pal << 1), 0);
  }

  restart();
}

void loop_user_1()
{

  static byte black_paint = 0;

  //if (random(300) == 0)
    
  if (PIXIE_SENSOR_TILTED) restart();

  if (!SHADER_PROGRAMS)
  {
    line(triangle[0].x, triangle[0].y, triangle[1].x, triangle[1].y, color);
    line(triangle[1].x, triangle[1].y, triangle[2].x, triangle[2].y, color);
    line(triangle[2].x, triangle[2].y, triangle[0].x, triangle[0].y, color);

  }
  else
  {
    line_accel(triangle[0].x, triangle[0].y, triangle[1].x, triangle[1].y, color);
    line_accel(triangle[1].x, triangle[1].y, triangle[2].x, triangle[2].y, color);
    line_accel(triangle[2].x, triangle[2].y, triangle[3].x, triangle[3].y, color);
    line_accel(triangle[3].x, triangle[3].y, triangle[4].x, triangle[4].y, color);
    line_accel(triangle[4].x, triangle[4].y, triangle[0].x, triangle[0].y, color);
  }
  
  color = (color + 1) & 3;
  byte i;
  for (i = 0; i < 3; i++) {
    triangle[i].x += triangle[i].xv;
    triangle[i].y += triangle[i].yv;
    if (triangle[i].x == 0 || triangle[i].x == 255)
      triangle[i].xv = -triangle[i].xv;
    if (triangle[i].y == 0 || triangle[i].y == 255)
      triangle[i].yv = -triangle[i].yv;
  }
  
  
  // complement to  a rectangle
    triangle[3].x += triangle[3].xv;
    triangle[3].y += triangle[3].yv;
    if (triangle[3].x == 0 || triangle[3].x == 255)
      triangle[3].xv = -triangle[3].xv;
    if (triangle[3].y == 0 || triangle[3].y == 255)
      triangle[3].yv = -triangle[3].yv;

  // or even more ?! :))
      triangle[4].x += triangle[4].xv;
    triangle[4].y += triangle[4].yv;
    if (triangle[4].x == 0 || triangle[4].x == 255)
      triangle[4].xv = -triangle[4].xv;
    if (triangle[4].y == 0 || triangle[4].y == 255)
      triangle[4].yv = -triangle[4].yv;
  

}

#endif

